package com.ruoyi.activiti.listener.execute;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 执行监听器
 * 1，变量的存取
 * 2，业务处理，如任务的统计时长
 * <p>
 * 不同的节点，实现不同的功能，比如发短信和发邮件。在不写监听类的前提下，可以使用自定义变量
 * 在页面中配置
 * @author: stone
 * @date: Created by 2023/2/12 16:59
 * @version: 1.0.0
 * @pakeage: com.ruoyi.activiti.listener.execute
 */
@Slf4j
public class LeaveExecuteListener implements ExecutionListener {

    private Expression sendType;

    private static final String START_EVENT = "start";
    private static final String END_EVENT = "end";

    @Override
    public void notify(DelegateExecution delegateExecution) {
        String eventName = delegateExecution.getEventName();

        log.info("事件名称 ==> {}", eventName);

        String id = delegateExecution.getProcessDefinitionId();

        log.info("流程实例id ==> {}", id);

        if (sendType.getValue(delegateExecution) != null) {
            String string = sendType.getValue(delegateExecution).toString();
            log.info("页面定义的变量 ===> {}", string);
        }




        execTime(delegateExecution);
    }


    //private Map<String, Long> timeMap = new HashMap<>();
    /**
     * 统计节点的时长
     *
     * @param delegateExecution
     */
    private Long l;
    private void execTime(DelegateExecution delegateExecution) {
        String eventName = delegateExecution.getEventName();

        if (START_EVENT.equals(eventName)) {
            l = System.currentTimeMillis();
        }
        else  {
            l = l - System.currentTimeMillis();
            log.info("该节点的时长是 ===> {}分钟",l/1000/60);
        }

    }
}
